// ActionScript file
package framework
{
	import framework.editors.EditorManager;
	import framework.editors.ObjectProperty;
	import framework.editors.PropertyListerManager;

	public function copy(fromObj:*, toObj:*, deep:Boolean = true):void
	{
		var fromObjProps:Array = PropertyListerManager.list(fromObj);
		var toObjProps:Array = PropertyListerManager.list(toObj);
		var props:Array = [];
		var toObjDymanic:Boolean = PropertyListerManager.isDynamic(toObj);
		for each (var op:ObjectProperty in fromObjProps)
		{
			if (toObjDymanic)
			{
				props.push(op);
			}
			else
			{
				for each (var toObjOp:ObjectProperty in toObjProps)
				{
					if (toObjOp.propName == op.propName)
					{
						props.push(op);
						break;
					}
				}
			}
		}
		for each (var selectedOp:ObjectProperty in props)
		{
			if (!selectedOp.isComplex || !deep)
			{
				selectedOp.reader = EditorManager.getBestReader(fromObj, selectedOp.
																propName);
				selectedOp.writer = EditorManager.getBestWriter(toObj, selectedOp.
																propName);
			}

		}

		//TODO caching

		for each (selectedOp in props)
		{
			if (deep && selectedOp.isComplex)
			{
				if (fromObj[selectedOp.propName] == null)
				{
					toObj[selectedOp.propName] = null;
				}
				else if (toObj[selectedOp.propName] != null)
				{
					copy(fromObj[selectedOp.propName], toObj[selectedOp.propName], true);
				}
			}
			else
			{
				var value:* = selectedOp.reader.read(fromObj, selectedOp.propName);
				selectedOp.writer.write(toObj, selectedOp.propName, value);
			}

		}

	}
}